package com.example.blog.mapper;

import com.example.blog.model.Blog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BlogMapper {
    // 查询博客列表
    @Select("select id, title, content, user_id, delete_flag, create_time, update_time " +
            "from blog where delete_flag = 0")
    List<Blog> selectAll();

    // 根据博客 id 查询博客信息
    @Select("select id, title, content, user_id, delete_flag, create_time, update_time " +
            "from blog where delete_flag = 0 and id = #{id}")
    Blog selectBlogById(Integer id);

    // 根据博客 id 更新博客信息
    Integer updateBlogById(Blog blog);

    // 插入一篇博客
    @Insert("insert into blog (title, content, user_id) values (#{title}, #{content}, #{userId})")
    Integer insertBlog(Blog blog);

    // 删除博客
    @Update("update blog set delete_flag = 1 where id = #{id}")
    Integer deleteBlogById(Integer id);

    @Select("select count(*) from blog where delete_flag = 0 and user_id = #{id}")
    Integer count(Integer id);
}
